import { _decorator, Component, Node, Pool, Prefab, v3, Vec3 } from 'cc';
import { ModuleBase } from '../../../FrameWork3.8.6/FrameWork/Module/ModuleBase';
import { ResMgr } from '../../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { UIUtils } from '../../../FrameWork3.8.6/FrameWork/UIFramework/UIUtils';
import { Emitter } from '../../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { Arrow } from '../Object/Arrow';
import { ShiBingBase } from '../Base/ShiBingBase';
const { ccclass, property } = _decorator;

@ccclass('FlyObjectLayer')
export class FlyObjectLayer extends ModuleBase {
    //弓箭的对象池
    private _arrowPool:Pool<Node>=null;

    //已经生成的弓箭的容器，方便统一管理
    private _mapArrow:Map<number,Arrow[]>=new Map();

    //对弓箭进行编号方便删除
    private _num:number=0;


    //初始化当前模块
    onInit(...rest: any[]): void {
        //初始化对象池
        let prefab=ResMgr.instance.getRes<Prefab>(BundleConfig.Game.name,"Arrow");
        this._arrowPool=UIUtils.initNodePool(prefab,5);

        //注册添加弓箭的方法
        //Emitter.instance.on("addArrow",this.addArrow,this)
    }

    //添加弓箭的方法
    addArrow(dir:number,attack:number,worldPos:Vec3,endPos:Vec3,owner:ShiBingBase){
        let arrowNode=this._arrowPool.alloc();
        arrowNode.active=true;
        arrowNode.parent=this.node;
        //设置位置
        arrowNode.setWorldPosition(worldPos);

        let arrow=arrowNode.getComponent(Arrow);
        let name="Arrow"+this._num;
        this._num++
        arrow.onInit(dir,attack,name,endPos,owner);
        
        let arrArrow:Arrow[]=this._mapArrow.get(dir);
        if(!arrArrow){
            arrArrow=[];
        }
        arrArrow.push(arrow);
        this._mapArrow.set(dir,arrArrow);
    }

    //销毁弓箭的方法
    deleteArrow(node:Node){
        let arrow=node.getComponent(Arrow);
        let dir=arrow.dir;
        
        let arrArrow=this._mapArrow.get(dir); 
        for(let i=0;i<arrArrow.length;i++){
            if(arrArrow[i].arrowName==arrow.arrowName){
                node.angle=90;
                let arrow=arrArrow[i];
                arrArrow.splice(i,1);
                this._mapArrow.set(arrow.dir,arrArrow);
                node.parent=null;
                node.active=false;
                this._arrowPool.free(node);
                
                
            }
        }

    }

    getArrow(owner:ShiBingBase){
        let arrArrow=this._mapArrow.get(owner.dirX)
        if(!arrArrow||arrArrow.length<=0){
            return null;
        }
        for(let arrow of arrArrow){
            if(arrow.owner==owner){
                return arrow;
            }
        }
        return null;
    }

    //统一调度容器中所有update方法
    onUpdate(dt: number, ...rest: any[]): void {
        if(Array.from(this._mapArrow.values()).length<=0){
            return;
        }
        for(let arrArrow of this._mapArrow.values()){
            for(let arrow of arrArrow){
                arrow.onUpdate(dt);
            }
        }
    }


}


